using System;
using System.Collections.Generic;
using System.Text;
using PPI.Entities.Interface;
using PPI.Entities.Kml;

namespace PPI.Entities.Polygon
{
	[Serializable]
	public class EUtility_Polygon : IGoogleEarth
	{
		#region Miembros Privados
		Int32 m_Id;
		string m_Name;
		double m_Altitude;
		double m_North;
		double m_East;
		double m_South;
		double m_West;
		EUtility_Polygon m_Utility_Polygon_ParentPolygon;
		EUtility_Polygon_PolygonType m_Utility_Polygon_PolygonType;
		Int32 m_LineColor;
		Int32 m_LineWidth;
		Int32 m_FillColor;
		Int32 m_FillOpacity;
		Int32 m_FontColor;
		string m_Font;
        List<EUtility_Polygon_Point> m_Utility_Polygon_Points;
		#endregion Miembros Privados

		#region Propiedades
		public Int32 Id
		{
			get { return m_Id; }
			set { m_Id = value; }
		}
		public string Name
		{
			get { return m_Name; }
			set { m_Name = value; }
		}
		public double Altitude
		{
			get { return m_Altitude; }
			set { m_Altitude = value; }
		}
		public double North
		{
			get { return m_North; }
			set { m_North = value; }
		}
		public double East
		{
			get { return m_East; }
			set { m_East = value; }
		}
		public double South
		{
			get { return m_South; }
			set { m_South = value; }
		}
		public double West
		{
			get { return m_West; }
			set { m_West = value; }
		}
		public EUtility_Polygon Utility_Polygon_ParentPolygon
		{
			get { return m_Utility_Polygon_ParentPolygon; }
			set { m_Utility_Polygon_ParentPolygon = value; }
		}
		public EUtility_Polygon_PolygonType Utility_Polygon_PolygonType
		{
			get { return m_Utility_Polygon_PolygonType; }
			set { m_Utility_Polygon_PolygonType = value; }
		}
		public Int32 LineColor
		{
			get { return m_LineColor; }
			set { m_LineColor = value; }
		}
		public Int32 LineWidth
		{
			get { return m_LineWidth; }
			set { m_LineWidth = value; }
		}
		public Int32 FillColor
		{
			get { return m_FillColor; }
			set { m_FillColor = value; }
		}
		public Int32 FillOpacity
		{
			get { return m_FillOpacity; }
			set { m_FillOpacity = value; }
		}
		public Int32 FontColor
		{
			get { return m_FontColor; }
			set { m_FontColor = value; }
		}
		public string Font
		{
			get { return m_Font; }
			set { m_Font = value; }
		}
        public List<EUtility_Polygon_Point> Utility_Polygon_Points
        {
            get { return m_Utility_Polygon_Points; }
            set { m_Utility_Polygon_Points = value; }
        }
        #endregion Propiedades

		#region Constructores
		#region summary
		/// <summary>
		/// Date: 04/28/2008
		/// Time: 16:07:37
		/// Generated by: VMCoder
		/// Generated using VirtualGenerator
		/// </summary>
		#endregion summary
		public EUtility_Polygon()
		{
			m_Id = Int32.MinValue;

		}

		#region summary
		/// <summary>
		/// Date: 04/28/2008
		/// Time: 16:07:37
		/// Generated by: VMCoder
		/// Generated using VirtualGenerator
		/// </summary>
		#endregion summary
		public EUtility_Polygon(Int32 utility_PolygonId)
			: this()
		{
			m_Id = utility_PolygonId;

		}

        #region summary
        /// <summary>
        /// Date: 04/28/2008
        /// Time: 16:07:37
        /// Generated by: VMCoder
        /// Generated using VirtualGenerator
        /// </summary>
        #endregion summary
        public EUtility_Polygon(Int32 utility_PolygonId, string name)
            : this(utility_PolygonId)
        {
            m_Name = name;

        }

        #region summary
        /// <summary>
        /// Date: 04/28/2008
        /// Time: 16:07:37
        /// Generated by: VMCoder
        /// Generated using VirtualGenerator
        /// </summary>
        #endregion summary
        public EUtility_Polygon(Int32 utility_PolygonId, string name, EUtility_Polygon utility_Polygon_ParentPolygon)
            : this(utility_PolygonId, name)
        {
            m_Utility_Polygon_ParentPolygon = utility_Polygon_ParentPolygon;
        }

        #region summary
		/// <summary>
		/// Date: 04/28/2008
		/// Time: 16:07:37
		/// Generated by: VMCoder
		/// Generated using VirtualGenerator
		/// </summary>
		#endregion summary
		public EUtility_Polygon(Int32 utility_PolygonId, string name, double altitude, double north, double east, double south, double west, EUtility_Polygon utility_Polygon_ParentPolygon, EUtility_Polygon_PolygonType utility_Polygon_PolygonType, Int32 lineColor, Int32 lineWidth, Int32 fillColor, Int32 fillOpacity, Int32 fontColor, string font)
            : this(utility_PolygonId, name, utility_Polygon_ParentPolygon)
		{
			m_Altitude = altitude;
			m_North = north;
			m_East = east;
			m_South = south;
			m_West = west;
			m_Utility_Polygon_PolygonType = utility_Polygon_PolygonType;
			m_LineColor = lineColor;
			m_LineWidth = lineWidth;
			m_FillColor = fillColor;
			m_FillOpacity = fillOpacity;
			m_FontColor = fontColor;
			m_Font = font;

		}

		#endregion Constructores

	    public EKmlFile ToKml()
	    {
            EKmlFile _file = new EKmlFile();
            _file.KMLCode = GetStyle();
            _file.KMLCode += GetPlaceMark();
            return _file;
	    }

	    public bool IsEmpty()
	    {
	        return this.m_Utility_Polygon_Points.Count <= 0;
	    }

        private string GetPointCordinates()
        {
            StringBuilder _Coordinates = new StringBuilder();
            foreach (EUtility_Polygon_Point point in m_Utility_Polygon_Points)
            {
                _Coordinates.Append(point.Longitude.ToString().Replace(",", ".") + "," + point.Latitude.ToString().Replace(",", ".") + ",1500 ");
            }
            return _Coordinates.ToString();
        }
        protected string GetPlaceMark()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Placemark id=\"" + GetPlacemarkId() + "\">");
            s.AppendLine(" <name>" + m_Name + "</name>");
            s.AppendLine(" <styleUrl>#map_" + GetStyleId() + "</styleUrl>");
            s.AppendLine(" <Polygon>");
            s.AppendLine("      <tessellate>1</tessellate>");
            s.AppendLine("      <extrude>1</extrude>");
            s.AppendLine("      <altitudeMode>relativeToGround</altitudeMode>");
            s.AppendLine("      <outerBoundaryIs>");
            s.AppendLine("          <LinearRing>");
            s.AppendLine("              <coordinates>");
            s.AppendLine("                  "+GetPointCordinates()+"");
            s.AppendLine("              </coordinates>");
            s.AppendLine("          </LinearRing>");
            s.AppendLine("      </outerBoundaryIs>");
            s.AppendLine(" </Polygon>");
            s.AppendLine("</Placemark>");
            return s.ToString();
        }
        protected string GetStyle()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine(" <Style id=\"" + GetStyleId() + "\">");
            s.AppendLine(" <IconStyle>");
            s.AppendLine("     <scale>1.3</scale>");
            s.AppendLine("     <Icon>");
            s.AppendLine("         <href>" + GetIcon() + "</href>");
            s.AppendLine("     </Icon>");
            s.AppendLine(" </IconStyle>");
            s.AppendLine(" <LineStyle>");
            s.AppendLine("      <color>ffffaaaa</color>");
            s.AppendLine("      <width>1.5</width>");
            s.AppendLine(" </LineStyle>");
            s.AppendLine(" <PolyStyle>");
            s.AppendLine("      <color>ffffaaaa</color>");
            s.AppendLine("      <fill>0</fill>");
            //s.AppendLine("      <colorMode>random</colorMode>");
            s.AppendLine(" </PolyStyle>");
            s.AppendLine("</Style>");
            s.AppendLine("<StyleMap id=\"map_" + GetStyleId() + "\">");
            s.AppendLine("  <Pair>");
            s.AppendLine("      <key>normal</key>");
            s.AppendLine("      <styleUrl>#" + GetStyleId() + "</styleUrl>");
            s.AppendLine("  </Pair>");
            s.AppendLine("</StyleMap>");
            return s.ToString();
        }
	    private string GetIcon()
        {
            return Constants.K_BASEREDMARK;
        }
        private string GetPlacemarkId()
        {
            return "Polygon_" + m_Id;
        }
        private string GetStyleId()
        {
            return "PolygonStyle_" + m_Id;
        }

	}
}

